package com.example.gulimall.ware.listener;

import com.example.gulimall.ware.service.WareSkuService;
import com.example.gulimall.ware.to.OrderTo;
import com.example.gulimall.ware.to.StockLockTo;
import com.example.gulimall.ware.vo.WareSkuLockVo;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;


@Component
@RabbitListener(queues = {"stock.release.stock.queue"})
public class StockReleaseListener {
    @Autowired
    private WareSkuService wareSkuService;
    @RabbitHandler
    public void stockReleaseHandler(StockLockTo stockLockTo, Message message, Channel channel) throws IOException {
        try{
            wareSkuService.unlock(stockLockTo);
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        }catch (Exception e){
            channel.basicReject(message.getMessageProperties().getDeliveryTag(),true);
        }
    }
    @RabbitHandler
    public void stockReleaseHandler(OrderTo orderTo, Message message, Channel channel) throws IOException {
        try{
            wareSkuService.unlock(orderTo);
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        }catch (Exception e){
            channel.basicReject(message.getMessageProperties().getDeliveryTag(),true);
        }
    }
    }
